package com.generate.library;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jregex.Pattern;

import com.generate.control.IReplace;

public class GenerateUtils {
	private GenerateUtils() {
	}

	public static String readFile(String path, Charset encoding)
			throws IOException {
		byte[] encoded = Files.readAllBytes(Paths.get(path));
		return new String(encoded, encoding);
	}

	public static String readFile(String path) throws IOException {
		return readFile(path, StandardCharsets.UTF_8);
	}

	public static List<File> listFilesForFolder(final File folder) {
		List<File> result = new ArrayList<File>();
		for (final File fileEntry : folder.listFiles()) {
			if (fileEntry.isDirectory()) {
				result.addAll(listFilesForFolder(fileEntry));
			} else {
				result.add(fileEntry);
			}
		}
		result.add(folder);
		return result;
	}

	public static void generate(File dstFolder, File srcFile)
			throws IOException {
		File dstFile = new File(dstFolder, replace(srcFile.getName()).replace(
				".template", ".impex"));
		if (srcFile.isDirectory()) {
			dstFile.mkdir();
		}
		System.out.print(srcFile.getPath() + "-->" + dstFile.getPath());
		if (srcFile.isDirectory()) {
			for (File file : srcFile.listFiles()) {
				generate(dstFile, file);
			}
		} else {
			String testString;
			testString = GenerateUtils.setNumberVariant(GenerateUtils.readFile(
					srcFile.getPath(), StandardCharsets.UTF_8), Long
					.parseLong(GenerateUtils.replace("@VARIANT_NUMBER@")));
			testString = GenerateUtils.setNumberImageDetail(testString, Long
					.parseLong(GenerateUtils.replace("@IMAGE_DETAIL_NUMBER@")));
			testString = GenerateUtils.replace(testString);
			FileWriter fw = new FileWriter(dstFile);
			fw.write(testString);
			fw.close();
		}

	}

	public static void generate(File folder) {
		try {
			File rootFolder = new File(
					"template\\@BASE_PRODUCT_CODE@-@DATE_NOW@");
			generate(folder, rootFolder);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String replace(String str) {
		String result = str;
		for (IReplace component : IReplace.lstReplaceComponent) {
			if (component.getFindText() != null
					&& component.getFindText().length() > 0 && component.getReplaceText()!=null) {
				Pattern pattern = new Pattern(component.getFindText(),
						Pattern.DOTALL);
				result = pattern.replacer(component.getReplaceText()).replace(
						result);
			}
		}

		result = result.replaceAll("@DATE_NOW@", new SimpleDateFormat(
				"yyyy-MM-dd").format(new Date()));
		return result;
	}

	public static String setNumberVariant(final String template,
			final Long numberVariant) {
		String result = template;
		if (numberVariant < 8) {
			Pattern pattern = new Pattern("@VARIANT8:START@.*?@VARIANT8:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberVariant < 7) {
			Pattern pattern = new Pattern("@VARIANT7:START@.*?@VARIANT7:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberVariant < 6) {
			Pattern pattern = new Pattern("@VARIANT6:START@.*?@VARIANT6:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberVariant < 5) {
			Pattern pattern = new Pattern("@VARIANT5:START@.*?@VARIANT5:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberVariant < 4) {
			Pattern pattern = new Pattern("@VARIANT4:START@.*?@VARIANT4:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberVariant < 3) {
			Pattern pattern = new Pattern("@VARIANT3:START@.*?@VARIANT3:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberVariant < 2) {
			Pattern pattern = new Pattern("@VARIANT2:START@.*?@VARIANT2:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		result = result.replaceAll("@VARIANT\\d:(?:START|END)@", "");
		return result;
	}

	public static String setNumberImageDetail(final String template,
			final Long numberDetail) {
		String result = template;
		if (numberDetail < 8) {
			Pattern pattern = new Pattern(
					"@IMAGE_DETAIL_NUMBER_8:START@.*?@IMAGE_DETAIL_NUMBER_8:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberDetail < 7) {
			Pattern pattern = new Pattern(
					"@IMAGE_DETAIL_NUMBER_7:START@.*?@IMAGE_DETAIL_NUMBER_7:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberDetail < 6) {
			Pattern pattern = new Pattern(
					"@IMAGE_DETAIL_NUMBER_6:START@.*?@IMAGE_DETAIL_NUMBER_6:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberDetail < 5) {
			Pattern pattern = new Pattern(
					"@IMAGE_DETAIL_NUMBER_5:START@.*?@IMAGE_DETAIL_NUMBER_5:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberDetail < 4) {
			Pattern pattern = new Pattern(
					"@IMAGE_DETAIL_NUMBER_4:START@.*?@IMAGE_DETAIL_NUMBER_4:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberDetail < 3) {
			Pattern pattern = new Pattern(
					"@IMAGE_DETAIL_NUMBER_3:START@.*?@IMAGE_DETAIL_NUMBER_3:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		if (numberDetail < 2) {
			Pattern pattern = new Pattern(
					"@IMAGE_DETAIL_NUMBER_2:START@.*?@IMAGE_DETAIL_NUMBER_2:END@",
					Pattern.DOTALL);
			result = pattern.replacer("").replace(result);
		}
		result = result.replaceAll("@IMAGE_DETAIL_NUMBER_\\d:(?:START|END)@",
				"");
		return result;
	}
}
